#! /bin/bash
# auto-install.sh
# automatically install software and configurations
# author: calvin zhang

for param in $*; do
	case $param in
		-f)
			F=1
			;;
		-c)
			CHK_SRC=1
			;;
		*)
			;;
	esac
done


logfile="log"
force_update=1

apt_sources_urls="mirrors.aliyun.com mirrors.ustc.edu.cn mirrors.163.com"

basic_progs="language-pack-zh ibus-pinyin"
dev_progs="git bash vim build-essential make openssh-server doxygen doxygen-gui graphviz"
amuse_progs="netease-music"
install_progs="${basic_progs} ${dev_progs} ${amuse_progs}"

# usage: basic_check prog config_file_path
function basic_check
{
	tab="\t"
	temprog=$(echo "$1" | tr [:lower:] [:upper:] | tr -d '\-')
	export ${temprog}_EXIST=0
	which $1 >/dev/null && export ${temprog}_EXIST=1
	apt list $1 | grep -e "installed" >/dev/null 2>/dev/null && export ${temprog}_EXIST=1
	[ "$F" = "1" ] && export ${temprog}_EXIST=0
	eval echo -e "\${tab} ${temprog}_EXIST = \${${temprog}_EXIST}"

	[ ! -n "$2" ] && return 0

	export ${temprog}_CONF_EXIST=0
	[ -f "$2" ] && export ${temprog}_CONF_EXIST=1
	[ "$F" = "1" ] && export ${temprog}_CONF_EXIST=0
	eval echo -e "\${tab} ${temprog}_CONF_EXIST = \${${temprog}_CONF_EXIST}"
}

function check_url
{
	wget --spider -q -o /dev/null --tries=1 -T 5 $1
	return $?
}

function perror
{
	echo -e -n "\e[1;31m">&2
	echo -e -n "$*" >&2
	echo -e "\e[0m" >&2
}

function pnotice
{
	echo -e -n "\e[1;32m"
	echo -e -n "$*"
	echo -e "\e[0m"
}

function choose_apt_source
{
	pnotice "choosing the best source..."
	tmp_min_delay=
	tmp_best_source=
	cur_source=
	best_source=
	for apt_source in ${apt_sources_urls}; do
		tmp_source_delay=$(ping ${apt_source} -c 10 -A | grep -e "rtt min\/avg" | sed -e 's;.*= [0-9,\.]*/\([0-9]*\).*;\1;g')
		pnotice "delay of ${apt_source} is: ${tmp_source_delay} ms"
		[ ! -n "${tmp_min_delay}" ] && tmp_min_delay=${tmp_source_delay} && tmp_best_source=${apt_source}
		(("${tmp_source_delay}" < "${tmp_min_delay}")) && tmp_min_delay=${tmp_source_delay} && tmp_best_source=${apt_source}
	done
	best_source=${tmp_best_source}
	pnotice "the choosen apt source is ${best_source}"
	cur_source=$(grep -e "http://.*/ubuntu" /etc/apt/sources.list | sed 's;.*http://\(.*\)/ubuntu.*;\1;g' | uniq)
	cur_source_array=(${cur_source})
	cur_source=${cur_source_array[0]}
	pnotice "current apt source is ${cur_source}"
	[ "${cur_source}" != "${best_source}" ] && sudo sed -i.bak -e "s;http://\(.*\)/ubuntu;http://${best_source}/ubuntu;g" /etc/apt/sources.list && sudo apt-get update
}

[ "$CHK_SRC" = "1" ] && choose_apt_source

for prog in ${install_progs}; do
	upper_prog=$(echo "[${prog}]" | tr [a-z] [A-Z])
	pnotice "${upper_prog}"
	case ${prog} in
		git)
			basic_check git ~/.gitconfig
			REMOTE_GITCONF="https://gitee.com/InvokerZhang/myconfigs/raw/master/gitconfig"
			[ ! "${GIT_EXIST}" = "1"  ] && sudo apt-get install git -y >/dev/null && pnotice "\t Install git"
			[ ! "${GIT_CONF_EXIST}" = "1" ] && wget ${REMOTE_GITCONF} -O ~/.gitconfig >/dev/null && pnotice "Config git"
			;;
		bash)
			basic_check bash ~/.bashrc
			REMOTE_BASHRC="https://gitee.com/InvokerZhang/myconfigs/raw/master/bashrc"
			[ ! "${BASH_EXIST}" = "1" ] && sudo apt-get install bash -y >/dev/null && pnotice "\t Install bash"
			check_url ${REMOTE_BASHRC}
			[ "$?" != "0" ] && perror "\t Invalid url: ${REMOTE_BASHRC}" && continue;
			[ ! "${BASH_CONF_EXIST}" = "1" ] && wget ${REMOTE_BASHRC} -O ~/.bashrc > /dev/null && pnotice "Config bash"
			;;
		vim)
			basic_check vim ~/.vimrc
			REMOTE_VIMRC="https://gitee.com/InvokerZhang/myconfigs/raw/master/vimrc"
			[ ! -f ~/.vim/bundle/Vundle.vim ] && git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim && vim +PluginInstall +wqa
			[ ! "${VIM_EXIST}" = "1" ] && sudo apt-get install vim -y >/dev/null && pnotice "\t Install vim"
			[ ! "${VIM_CONF_EXIST}" = "1" ] && wget ${REMOTE_VIMRC} -O ~/.vimrc >/dev/null && pnotice "\t Config vim"
			[ ! -d "~/.vim/bundle" ] && git clone https://github.com/VundleVim/Vundle.vim ~/.vim/bundle/Vundle.vim && vim +PluginInstall +qall
			;;
		make)
			basic_check make
			[ ! "${MAKE_EXIST}" = "1" ] && sudo apt-get install make -y >/dev/null && pnotice "\t Install make"
			;;
		build-essential)
			basic_check build-essential
			[ ! "${BUILDESSENTIAL_EXIST}" = "1" ] && sudo apt-get install build-essential -y >/dev/null && pnotice "\t Dummy Install build-essential"
			;;
		openssh-server)
			basic_check openssh-server
			[ ! "${OPENSSHSERVER_EXIST}" = "1" ] && sudo apt-get install openssh-server -y >/dev/null && pnotice "\t Install openssh-server"
			;;
		language-pack-zh)
			basic_check language-pack-zh
			basic_check language-pack-gnome-zh
			[ ! "${LANGUAGEPACKZH_EXIST}" = "1" ] && sudo apt-get install language-pack-zh-hans* -y
			[ ! "${LANGUAGEPACKGNOMEZH_EXIST}" = "1" ] && sudo apt-get install language-pack-gnome-zh-hans* -y
			;;
		ibus-pinyin)
			basic_check ibus-pinyin
			[ ! "${IBUSPINYIN_EXIST}" = "1" ] && sudo apt-get install ibus-pinyin -y && ibus setup
			;;
		*)
			perror "Not supported prog: ${prog}"
			;;
	esac
done


<<'COMMENTT'
# clear error.log
echo "" > error.log

progs="zsh vim vim-gnome vim-gtk3 vim-gui-common nodejs npm git make build-essential doxygen doxygen-gui graphviz ibus-sunpinyin"

sudo apt-get install $progs 2>> error.log

# configure zsh
if [ ! -e ~/.oh-my-zsh ]; then
	echo "++configure oh-my-zsh..."
	git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
	cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
	if [ ! -e /bin/zsh ]; then
		zsh='which zsh';
		sudo ln -s $zsh /bin/zsh;
	fi
	sudo chsh -s /bin/zsh
else
	echo "++zsh has been configured"
fi 2>> error.log

# configure vim
if [ ! -e ~/.vimrc ]; then
	mkdir -p ~/workspace
	git clone http://gitee.com/InvokerZhang/vim.git ~/workspace/vim
	cp ~/workspace/vim/vimrc ~/.vimrc
else
	echo "++vimrc exists!"
fi 2>> error.log
if [ ! -e ~/.vim/bundle/Vundle.vim ]; then
	echo "++Installing vundle"
	mkdir -p ~/.vim/bundle/
	git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
else
	echo "++vundle has been installed"
fi 2>> error.log
if ! which instant-markdown-d; then
	sudo npm -g install instant-markdown-d
fi 2>> error.log
vim +PluginInstall +qall

# configure git
echo "++configuring git..."
git config --global user.name "Invoker Zhang"
git config --global user.email "inovkerzhang@outlook.com"
git config --global core.editor vim

# install firefox-cn
if [ ! -e ~/firefox ] || [ ! -e /usr/share/applications/firefox.desktop ]; then
	echo "++installing firefox"
	wget https://download-ssl.firefox.com.cn/releases/firefox/59.0/zh-CN/Firefox-latest-x86_64.tar.bz2
	tar -xvf Firefox-latest-x86_64.tar.bz2
	mv firefox ~
	rm Firefox-latest-x86_64.tar.bz2
	if [ -e /usr/bin/firefox ];then
		sudo apt-get remove firefox
	fi
	sudo ln -s /home/$USER/firefox/firefox /usr/bin/firefox
	sudo echo "[Desktop Entry]" >> /usr/share/applications/firefox.desktop
	sudo echo "Name=Firefox" >> /usr/share/applications/firefox.desktop
	sudo echo "Exec=firefox" >> /usr/share/applications/firefox.desktop
	sudo echo "Icon=firefox" >> /usr/share/applications/firefox.desktop
	sudo echo "Type=Application" >> /usr/share/applications/firefox.desktop
	sudo cp ~/firefox/browser/chrome/icons/default/default48.png /usr/share/icons/hicolor/48x48/apps/firefox.png
else
	echo "++firefox-cn has been installed and configured"
fi 2>> error.log

# install youdao-dict
if ! which youdao-dict ; then
	echo "++Installing youdao-dict"
	wget http://codown.youdao.com/cidian/linux/youdao-dict_1.1.0-0-deepin_amd64.deb
	sudo dpkg -i youdao-dict_1.1.0-0-deepin_amd64.deb
	sudo apt-get install -f
	sudo dpkg -i youdao-dict_1.1.0-0-deepin_amd64.deb
	rm youdao-dict_1.1.0-0-deepin_amd64.deb
else
	echo "++youdao-dict has been installed"
fi 2>>error.log

# install netease-cloud-music
if ! which netease-cloud-music ; then
	echo "++Installing netease-cloud-music"
	wget http://d1.music.126.net/dmusic/netease-cloud-music_1.1.0_amd64_ubuntu.deb
	sudo dpkg -i netease-cloud-music_1.1.0_amd64_ubuntu.deb
	sudo apt-get install -f
	sudo dpkg -i netease-cloud-music_1.1.0_amd64_ubuntu.deb
	rm netease-cloud-music_1.1.0_amd64_ubuntu.deb
else
	echo "++netease-cloud-music has been installed"
fi 2>>error.log

# install indicator-sysmonitor
if ! which indicator-sysmonitor ; then
	echo "++Installing indicator-sysmonitor"
	sudo apt-get install python3-psutil curl git gir1.2-appindicator3-0.1
	git clone https://github.com/fossfreedom/indicator-sysmonitor.git
	cd indicator-sysmonitor
	sudo make install
	nohup indicator-sysmonitor &
	cd ..
	mv indicator-sysmonitor ~
else
	echo "++indicator-sysmonitor has been installed"
fi 2>>error.log
COMMENTT
